1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
| localStorage.setItem("key","value"); localStorage.key = "value" localStorage.getItem("key"); localStorage.key; localStorage.removeItem("key") <!--more-->
---
var students = { liyang:{name:"liyang",age:17}, lilei:{name:"lilei",age:18} } students = JSON.stringify(students); localStorage.setItem("students",students);
--
var students = localStorage.getItem("students"); students = JSON.parse(students);
--- utils = { setParam : function (name,value){ localStorage.setItem(name,value) }, getParam : function(name){ return localStorage.getItem(name) } } product={ id:0, name:"", num:0, price:0.00 }; orderdetail={ username:"", phone:"", address:"", zipcode:"", totalNumber:0, totalAmount:0.00 } cart = { addproduct: function (product) { var ShoppingCart = utils.getParam("ShoppingCart"); if (ShoppingCart == null || ShoppingCart == "") { var jsonstr = { "productlist": [{ "id": product.id, "name": product.name, "num": product.num, "price": product.price}], "totalNumber": product.num, "totalAmount": (product.price * product.num) }; utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr)); } else { var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length)); var productlist = jsonstr.productlist; var result = false; for (var i in productlist) { if (productlist[i].id == product.id) { productlist[i].num = parseInt(productlist[i].num) + parseInt(product.num); result = true; } } if (!result) { productlist.push({ "id": product.id, "name": product.name, "num": product.num, "price": product.price }); } jsonstr.totalNumber = parseInt(jsonstr.totalNumber) + parseInt(product.num); jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) + (parseInt(product.num) * parseFloat(product.price)); orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr)); } }, updateproductnum: function (id, num) { var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length)); var productlist = jsonstr.productlist; for (var i in productlist) { if (productlist[i].id == id) { jsonstr.totalNumber = parseInt(jsonstr.totalNumber) + (parseInt(num) - parseInt(productlist[i].num)); jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) + ((parseInt(num) * parseFloat(productlist[i].price)) - parseInt(productlist[i].num) * parseFloat(productlist[i].price)); productlist[i].num = parseInt(num); orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr)); return; } } }, getproductlist: function () { var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length)); var productlist = jsonstr.productlist; orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; return productlist; }, existproduct: function (id) { var result = false; var ShoppingCart = utils.getParam("ShoppingCart"); if (ShoppingCart != null) { var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length)); var productlist = jsonstr.productlist; for (var i in productlist) { if (productlist[i].id == id) { result = true; } } } return result; }, deleteproduct: function (id) { var ShoppingCart = utils.getParam("ShoppingCart"); var jsonstr = JSON.parse(ShoppingCart.substr(1, ShoppingCart.length)); var productlist = jsonstr.productlist; var list = []; for (var i in productlist) { if (productlist[i].id == id) { jsonstr.totalNumber = parseInt(jsonstr.totalNumber) - parseInt(productlist[i].num); jsonstr.totalAmount = parseFloat(jsonstr.totalAmount) - parseInt(productlist[i].num) * parseFloat(productlist[i].price); } else { list.push(productlist[i]); } } jsonstr.productlist = list; orderdetail.totalNumber = jsonstr.totalNumber; orderdetail.totalAmount = jsonstr.totalAmount; utils.setParam("ShoppingCart", "'" + JSON.stringify(jsonstr)); } };
-----------
var product = { 'id': id, 'name': 'hhh', 'num':jq('#text-4').val(), 'price':199.9 };
cart.addproduct(product); var productlist=cart.getproductlist(); alert('', '商品:'+productlist[0].id+' '+productlist[0].name+' '+productlist[0].num+' '+productlist[0].price, '确定');
|